package com.ziyunying.weather.api

import android.annotation.SuppressLint
import android.content.Context
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.ziyunying.weather.common.utils.Logger
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class AMapLocationApi @Inject constructor(
    private val context: Context
) {
    init {
        // 初始化高德地图SDK
        try {
            AMapLocationClient.updatePrivacyShow(context, true, true)
            AMapLocationClient.updatePrivacyAgree(context, true)
        } catch (e: Exception) {
            Logger.i(message = "初始化高德地图SDK失败: ${e.message}")
        }
    }

    @SuppressLint("MissingPermission")
    fun getLocation(
        onSuccess: (location: AMapLocation) -> Unit,
        onFailure: (errorCode: Int, errorInfo: String) -> Unit
    ) {
        try {
            // 创建定位客户端
            val locationClient = AMapLocationClient(context)

            // 创建定位选项
            val locationOption = AMapLocationClientOption().apply {
                // 设置定位模式为高精度模式
                locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy
                // 设置是否返回地址信息
                isNeedAddress = true
                // 设置是否单次定位
                isOnceLocation = true
                // 设置定位请求超时时间
                httpTimeOut = 10000
            }

            // 设置定位参数
            locationClient.setLocationOption(locationOption)

            // 设置定位回调
            locationClient.setLocationListener { location ->
                if (location != null) {
                    if (location.errorCode == 0) {
                        // 定位成功，返回完整的AMapLocation对象
                        onSuccess(location)
                        // 定位成功: 经度=118.754018, 纬度=31.959507, 国家=中国, 省份=江苏省, 城市=南京市, 区县=雨花台区, 街道=春晓路, 门牌号=24号, 城市编码=025, 区域编码=320114,
                        // 记录更详细的位置信息
                        Logger.logResponse(
                            location, "AMapLocation", "定位成功: " +
                                    "经度=${location.longitude}, " +
                                    "纬度=${location.latitude}, " +
                                    "国家=${location.country}, " +
                                    "省份=${location.province}, " +
                                    "城市=${location.city}, " +
                                    "区县=${location.district}, " +
                                    "街道=${location.street}, " +
                                    "门牌号=${location.streetNum}, " +
                                    "城市编码=${location.cityCode}, " +
                                    "区域编码=${location.adCode}, " +
                                    "AOI信息=${location.aoiName}"
                        )
                    } else {
                        // 定位失败
                        onFailure(location.errorCode, location.errorInfo)
                    }
                } else {
                    // 定位结果为空
                    onFailure(-1, "定位结果为空")
                }

                // 停止定位并释放资源
                locationClient.stopLocation()
                locationClient.onDestroy()
            }

            // 启动定位
            locationClient.startLocation()
        } catch (e: Exception) {
            // 初始化或定位过程中出现异常
            onFailure(-1, "定位异常: ${e.message}")
        }
    }
}